package com.senyin.servlet.position;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.senyin.common.BaseResponse;
import com.senyin.common.ErrorCode;
import com.senyin.common.ResultUtils;
import com.senyin.entity.Dept;
import com.senyin.entity.Position;
import com.senyin.service.DeptService;
import com.senyin.service.PositionService;
import com.senyin.service.impl.DeptServiceImpl;
import com.senyin.service.impl.PositionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class PositionModServlet extends HttpServlet {
    private PositionService positionService = new PositionServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        String psId = req.getParameter("psId");
        String psName = req.getParameter("psName");
        String psDes = req.getParameter("psDes");
        Position position = new Position(psId,psName, psDes);
        BaseResponse baseResponse = null;
        try {
            baseResponse = positionService.modPosition(position, req);
        } catch (SQLException e) {
            baseResponse = ResultUtils.error(ErrorCode.SYSTEM_ERROR,"error");
            resp.getWriter().write(objectMapper.writeValueAsString(baseResponse));
        }
        resp.getWriter().write(objectMapper.writeValueAsString(baseResponse));
    }
}
